#!/bin/sh

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

# 日志函数
log_info() {
    echo -e "${GREEN}[信息]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[警告]${NC} $1"
}

log_error() {
    echo -e "${RED}[错误]${NC} $1"
}

# 选择遥控器类型
select_remote_type() {
    echo -e "${YELLOW}[选择]${NC} 请选择遥控器类型:"
    echo "1) 中国移动机顶盒遥控器"
    echo "2) 腾讯极光4pro遥控器"
    echo
    
    read -p "请输入数字选择 (1-2): " choice
    
    case $choice in
        1)
            REMOTE_TYPE="中国移动机顶盒遥控器"
            HWDB_URL="https://gitee.com/glsimon/magic-button/raw/master/ce-bt.hwdb"
            XML_URL="https://gitee.com/glsimon/magic-button/raw/master/ce-bt.xml"
            log_info "已选择: $REMOTE_TYPE"
            return 0
            ;;
        2)
            REMOTE_TYPE="腾讯极光4pro遥控器"
            HWDB_URL="https://gitee.com/glsimon/magic-button/raw/master/4pro/4pro.hwdb"
            XML_URL="https://gitee.com/glsimon/magic-button/raw/master/4pro/gen.xml"
            log_info "已选择: $REMOTE_TYPE"
            return 0
            ;;
        *)
            log_error "无效选择，请输入1或2"
            return 1
            ;;
    esac
}

# 创建必要的目录
create_directories() {
    log_info "创建必要的目录..."
    mkdir -p /storage/.config/hwdb.d
    mkdir -p /storage/.kodi/userdata/keymaps
    mkdir -p /storage/.kodi/userdata/addon_data/script.magic.button
    mkdir -p /storage/downloads
    mkdir -p /storage/.kodi/addons
}

# 下载文件函数
download_file() {
    local url=$1
    local dest=$2
    local filename=$(basename "$url")
    
    # 直接使用raw URL下载
    log_info "正在下载 $filename 到 $dest..."
    raw_url="${url/\/blob\//\/raw\/}"
    wget -q -O "$dest/$filename" "$raw_url"
    
    if [ $? -ne 0 ]; then
        log_error "下载 $filename 失败"
        return 1
    fi
    
    log_info "下载 $filename 成功"
    return 0
}

# 配置硬件数据库
configure_hwdb() {
    log_info "配置硬件数据库..."
    
    download_file "$HWDB_URL" "/storage/.config/hwdb.d"
    if [ $? -ne 0 ]; then
        log_error "硬件数据库配置失败"
        return 1
    fi
    
    log_info "更新硬件数据库..."
    systemd-hwdb update
    if [ $? -ne 0 ]; then
        log_error "systemd-hwdb update 失败"
        return 1
    fi
    
    log_info "触发udev规则..."
    udevadm trigger
    if [ $? -ne 0 ]; then
        log_error "udevadm trigger 失败"
        return 1
    fi
    
    log_info "硬件数据库配置成功"
    return 0
}

# 配置Kodi按键映射
configure_kodi_keymaps() {
    log_info "配置Kodi按键映射..."
    
    # 备份现有xml文件
    log_info "备份现有按键映射文件..."
    for file in /storage/.kodi/userdata/keymaps/*.xml; do
        if [ -f "$file" ]; then
            log_info "备份 $file 到 ${file}.bak"
            cp -f "$file" "${file}.bak"
        fi
    done
    
    # 下载新的按键映射文件
    download_file "$XML_URL" "/storage/.kodi/userdata/keymaps"
    if [ $? -ne 0 ]; then
        log_error "Kodi按键映射配置失败"
        return 1
    fi
    
    log_info "Kodi按键映射配置成功"
    return 0
}

# 安装Kodi插件
install_kodi_addon() {
    log_info "准备Kodi插件安装文件..."
    
    download_file "https://gitee.com/glsimon/magic-button/raw/master/script.magic.button.zip" "/storage/downloads"
    if [ $? -ne 0 ]; then
        log_error "插件下载失败"
        return 1
    fi
    
    # 确保目标目录存在
    mkdir -p /storage/.kodi/addons
    
    # 解压插件到Kodi插件目录
    log_info "解压插件到Kodi插件目录..."
    unzip -o -q /storage/downloads/script.magic.button.zip -d /storage/.kodi/addons/
    
    if [ $? -ne 0 ]; then
        log_error "插件解压失败"
        return 1
    fi
    
    # 验证插件目录是否存在
    plugin_dir="/storage/.kodi/addons/script.magic.button"
    if [ ! -d "$plugin_dir" ]; then
        log_error "插件目录不存在: $plugin_dir"
        return 1
    fi
    
    log_info "插件文件已成功准备好"
    log_warning "由于自动启用插件可能失败，请在Kodi重启后手动启用插件："
    log_warning "1. 打开『设置』"
    log_warning "2. 进入『插件』>『我的插件』>『全部』"
    log_warning "3. 找到『Magic Button』插件并启用"
    
    return 0
}

# 重启Kodi
restart_kodi() {
    log_info "重启Kodi..."
    
    # 使用systemctl重启Kodi
    systemctl restart kodi
    
    if [ $? -ne 0 ]; then
        log_error "Kodi重启失败"
        return 1
    fi
    
    log_info "Kodi重启命令已执行"
    log_warning "请等待Kodi完全启动后，按照上述步骤手动启用Magic Button插件"
    
    return 0
}

# 主函数
main() {
    log_info "开始配置CoreELEC一键音轨字幕插件..."
    
    select_remote_type || return 1
    create_directories || return 1
    configure_hwdb || return 1
    configure_kodi_keymaps || return 1
    # configure_kodi_skin || return 1  # 已移除JSON配置
    install_kodi_addon || return 1
    restart_kodi || return 1
    
    log_info "CoreELEC一键音轨字幕插件配置完成！"
    log_info "请测试你的$REMOTE_TYPE功能是否正常"
    return 0
}

# 执行主函数
main
